$(function () {
    $("#jqGrid").Grid({
        url: '../agent/agentList',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '供应商商家ID', name: 'vendorRootDeptId', index: 'vendor_root_dept_id', hidden: true},
			{label: '代理商名称', name: 'agentRootDeptName', index: 'agentRootDeptName', width: 80},
			{label: '申请人名称', name: 'applyUserName', index: 'applyUserName', width: 80},
            {label: '申请人手机', name: 'applyUserMobile', index: 'applyUserMobile', width: 80},
			{label: '申请时间', name: 'applyTime', index: 'apply_time', width: 80, formatter: function (value) { return transDate(value)}},
			{label: '审核时间', name: 'certTime', index: 'cert_time', width: 80, formatter: function (value) { return transDate(value)}},
            {label: '认证状态', name: 'isCert', index: 'is_cert', width: 80, align: 'center', formatter: function (value, options, row) {
            		if(value == 0){
            			return '<span class="label label-warning">待审核</span>'
					}
                    if(value == 1){
                        return '<span class="label label-success">已通过</span>'
                    }
                    if(value == 2){
                        return '<span class="label label-danger">已否决</span>'
                    }
                }
             },
			{label: '认证等级', name: 'certLevel', index: 'cert_level', width: 80, align: 'center', formatter: function (value, options, row) {
                    if (value) {
                        if (value == 0) {
                            return '初级代理商'
                        }
                        if (value == 1) {
                            return '中级代理商'
                        }
                        if (value == 2) {
                            return '高级代理商'
                        }
                    }else{
                    	return '-'
					}
                }
			}
		]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		agent: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.agent = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.agent.id == null ? "../agent/save" : "../agent/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.agent),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../agent/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
        rejectApply: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null){
                return;
            }

            confirm('确定要否决选中的代理申请？', function () {
                Ajax.request({
                    url: "../agent/rejectApply",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        passApply: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null){
                return;
            }

            confirm('确定要通过选中的代理申请？', function () {
                Ajax.request({
                    url: "../agent/passApply",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
		getInfo: function(id){
            Ajax.request({
                url: "../agent/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.agent = r.agent;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});